package main

import (
	"flag"
	"github.com/Terry-Mao/goconf"	//这个之后还要学一下
	// "runtime"
	"time"
)

var (
	Conf *Config
	confFile string
)

func init() {
	flag.StringVar(&confFile, "c", "./userservice.conf", " set the path of the config file of userservice")
}

type Config struct{
	// base config for the server
	User          string   `goconf:"base:user"`
	PidFile       string   `goconf:"base:pidfile"`
	Dir           string   `goconf:"base:dir"`
	Log           string   `goconf:"base:log"`
	MaxProc       int      `goconf:"base:maxproc"`
	TCPBind       []string `goconf:"base:tcp.bind:,"`
	WebsocketBind []string `goconf:"base:websocket.bind:,"`
	RPCBind       []string `goconf:"base:rpc.bind:,"`
	PprofBind     []string `goconf:"base:pprof.bind:,"`
	StatBind      []string `goconf:"base:stat.bind:,"`

	// mongodb instance config
	MongoNodeAddress string `goconf:"mongodb:address"`

	// channel
	SndbufSize              int           `goconf:"channel:sndbuf.size:memory"`
	RcvbufSize              int           `goconf:"channel:rcvbuf.size:memory"`
	Proto                   []string      `goconf:"channel:proto:,"`
	BufioInstance           int           `goconf:"channel:bufio.instance"`
	BufioNum                int           `goconf:"channel:bufio.num"`
	TCPKeepalive            bool          `goconf:"channel:tcp.keepalive"`
	MaxSubscriberPerChannel int           `goconf:"channel:maxsubscriber"`
	ChannelBucket           int           `goconf:"channel:bucket"`
	Auth                    bool          `goconf:"channel:auth"`
	TokenExpire             time.Duration `goconf:"-"`
	MsgBufNum               int           `goconf:"channel:msgbuf.num"`
}

// InitConfig get a new Config struct.
func InitConfig() error {
	Conf = &Config{
		MongoNodeAddress : "127.0.0.1:27017",
	}
	c := goconf.New()
	if err := c.Parse(confFile); err != nil {
		return err
	}
	if err := c.Unmarshal(Conf); err != nil {
		return err
	}
	return nil
}
